package com.ruan.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruan.common.Code;
import com.ruan.common.Result;
import com.ruan.mapper.AdminMapper;
import com.ruan.mapper.CommonUserMapper;
import com.ruan.mapper.EmployeeMapper;
import com.ruan.pojo.*;
import com.ruan.pojoExt.AdminExt;
import com.ruan.pojoExt.CommonUserExt;
import com.ruan.pojoExt.EmployeeExt;
import com.ruan.service.*;
import com.ruan.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;

import org.apache.http.HttpStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * 用户
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private CommonUserService commonUserService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private CommonUserMapper commonUserMapper;

    @Autowired
    private PositionService positionService;

    @Autowired
    private AdminMapper adminMapper;

    @Resource
    private BCryptPasswordEncoder encoder;

    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @GetMapping("/admin/selectAccountByPhone")
    public Result selectAdminAccountByPhone(String phone){
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getPhone,phone);
        queryWrapper.notIn(Admin::getStatus,"禁用");
        Integer count = adminMapper.selectCount(queryWrapper);
        if(count == 0){
            return new Result(Code.SELECT_ERR,"该手机号未注册");
        }else if(count > 1){
            return new Result(Code.SELECT_ERR,"该手机号已注册多个管理员，请联系管理员");
        }else {
            Admin admin = adminMapper.selectOne(queryWrapper);
            admin.setPassword("***");
            return new Result(Code.SELECT_OK,admin);
        }
    }

    @PostMapping("/admin/appForAccount")
    @Transactional
    public Result appForAdminAccount(@RequestParam String phone,
                                     @RequestBody Admin admin){
        System.out.println("admin是"+admin);
        if(phone == null || phone.equals("")){
            return new Result(Code.SELECT_ERR,"旧手机号为空");
        }
//        if(admin.getUsername().contains("adminRole")){
//            return new Result(Code.SELECT_ERR,"该用户名已存在");
//        }
        LambdaQueryWrapper<Admin> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.ne(Admin::getId,admin.getId());
        //除开本账号，确保用户名与其它账户不重复
        queryWrapper1.eq(Admin::getUsername,admin.getUsername()+"adminRole");
        Integer count1 = adminMapper.selectCount(queryWrapper1);
        if(count1 > 0){
            return new Result(Code.SELECT_ERR,"该用户名已存在");
        }
        LambdaQueryWrapper<Admin> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.ne(Admin::getId,admin.getId());
        //除开本账号，确保用户名与其它账户不重复
        queryWrapper2.eq(Admin::getPhone,admin.getPhone());
        Integer count2 = adminMapper.selectCount(queryWrapper2);
        if(count2 > 0){
            return new Result(Code.SELECT_ERR,"该手机号已存在");
        }
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getId,admin.getId());
        queryWrapper.eq(Admin::getPhone,phone);
        Integer count = adminMapper.selectCount(queryWrapper);
        if(count == 0){
            return new Result(Code.SELECT_ERR,"该用户不存在");
        }else if(count > 1){
            return new Result(Code.SELECT_ERR,"该用户重复，请联系管理员");
        }else {
            Admin admin1 = adminMapper.selectOne(queryWrapper);
            //如果修改了用户名，就加上admin，否则不用，它本来就有admin
            if(admin.getUsername().equals(admin1.getUsername())
                    && admin.getPhone().equals(admin1.getPhone())){
                return new Result(Code.UPDATE_OK,"账号信息未变更！");
            }
            if(!admin.getUsername().equals(admin1.getUsername())){
                admin1.setUsername(admin.getUsername()+"adminRole");
            }
            if(!admin.getPhone().equals(admin1.getPhone())){
                admin1.setPhone(admin.getPhone());
            }
            adminMapper.updateById(admin1);
            //如果手机号也改了，员工表里的手机号也要修改
            return new Result(Code.UPDATE_OK,"账号信息修改成功！");
        }
    }

    @GetMapping("/selectAccountByPhone")
    public Result selectAccountByPhone(String phone){
        LambdaQueryWrapper<CommonUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommonUser::getPhone,phone);
        queryWrapper.notIn(CommonUser::getStatus,"禁用");
        Integer count = commonUserMapper.selectCount(queryWrapper);
        if(count == 0){
            return new Result(Code.SELECT_ERR,"该手机号未注册");
        }else if(count > 1){
            return new Result(Code.SELECT_ERR,"该手机号已注册多个员工，请联系管理员");
        }else {
            CommonUser commonUser = commonUserMapper.selectOne(queryWrapper);
            commonUser.setPassword("***");
            return new Result(Code.SELECT_OK,commonUser);
        }
    }

    @PostMapping("/appForAccount")
    @Transactional
    public Result appForAccount(@RequestParam String phone, @RequestBody CommonUser commonUser){
        if(phone == null || phone.equals("")){
            return new Result(Code.SELECT_ERR,"旧手机号为空");
        }
        if(commonUser.getUsername().contains("adminRole")){
            return new Result(Code.SELECT_ERR,"该用户名已存在");
        }
        LambdaQueryWrapper<CommonUser> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.ne(CommonUser::getId,commonUser.getId());
        //除开本账号，确保用户名与其它账户不重复
        queryWrapper1.eq(CommonUser::getUsername,commonUser.getUsername());
        Integer count0 = commonUserMapper.selectCount(queryWrapper1);
        if(count0 > 0){
            return new Result(Code.SELECT_ERR,"该用户名已存在");
        }

        LambdaQueryWrapper<CommonUser> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.ne(CommonUser::getId,commonUser.getId());
        //除开本账号，确保用户名与其它账户不重复
        queryWrapper2.eq(CommonUser::getPhone,commonUser.getPhone());
        Integer count2 = commonUserMapper.selectCount(queryWrapper2);
        if(count2 > 0){
            return new Result(Code.SELECT_ERR,"该手机号已存在");
        }

        LambdaQueryWrapper<CommonUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommonUser::getId,commonUser.getId());
        queryWrapper.eq(CommonUser::getPhone,phone);
        Integer count = commonUserMapper.selectCount(queryWrapper);
        if(count == 0){
            return new Result(Code.SELECT_ERR,"该用户不存在");
        }else if(count > 1){
            return new Result(Code.SELECT_ERR,"该用户重复，请联系管理员");
        }else {
            CommonUser commonUser1 = commonUserMapper.selectOne(queryWrapper);
            if(commonUser.getPhone().equals(commonUser1.getPhone()) &&
                    commonUser.getUsername().equals(commonUser1.getUsername())){
                return new Result(Code.UPDATE_OK,"账号信息未变更！");
            }

            if(!commonUser.getUsername().equals(commonUser1.getUsername())){
                commonUser1.setUsername(commonUser.getUsername());
            }

            //如果手机号改了，员工表里的手机号也要修改
//            System.out.println("phone是"+phone+"commonUser.getPhone()是"+commonUser.getPhone());
//            System.out.println("commonUser1.getPhone()是"+commonUser1.getPhone());

            if(!commonUser1.getPhone().equals(commonUser.getPhone())){
                commonUser1.setPhone(commonUser.getPhone());
                LambdaQueryWrapper<Employee> employeeQueryWrapper = new LambdaQueryWrapper<>();
                employeeQueryWrapper.eq(Employee::getPhone,phone);
                Integer count1 = employeeMapper.selectCount(employeeQueryWrapper);
                if(count1 == 0){
                    return new Result(Code.SELECT_ERR,"该用户未填写入职申请");
                }else if(count1 > 1){
                    return new Result(Code.SELECT_ERR,"该用户绑定员工重复，请联系管理员");
                }
                Employee employee = employeeMapper.selectOne(employeeQueryWrapper);
                if(employee!=null){
                    commonUserMapper.updateById(commonUser1);
                    employee.setPhone(commonUser.getPhone());
                    employeeMapper.updateById(employee);
                    return new Result(Code.UPDATE_OK,"账号信息修改成功！");
                }
            }

            commonUserMapper.updateById(commonUser1);
            return new Result(Code.UPDATE_OK,"账号信息修改成功！");
        }
    }


    @PostMapping("/admin/register")
    public Result register(@RequestBody Admin admin){
        if(admin.getUsername()==null || admin.getUsername().equals("")) {
            return new Result(Code.SELECT_ERR,"用户名不能为空");
        }
        if(admin.getPassword()==null || admin.getPassword().equals("")){
            return new Result(Code.SELECT_ERR,"密码不能为空");
        }
        if(admin.getPhone()==null || admin.getPhone().equals("")){
            return new Result(Code.SELECT_ERR,"手机号不能为空");
        }
        if (admin.getUsername().contains("adminRole")){
            return new Result(Code.REGISTER_ERR,"该用户名已被占用！");
        }
        admin.setUsername(admin.getUsername()+"adminRole");
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername,admin.getUsername());
        Integer count = adminMapper.selectCount(queryWrapper);
        if(count>0){
            return new Result(Code.SELECT_ERR,"该用户名已存在");
        }
        LambdaQueryWrapper<Admin> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Admin::getPhone,admin.getPhone());
        Integer count1 = adminMapper.selectCount(queryWrapper1);
        if(count1>0){
            return new Result(Code.SELECT_ERR,"该手机号已注册");
        }

        /** encode()加密 */
        String passwordEncode = encoder.encode(admin.getPassword());
        admin.setPassword(passwordEncode);
        admin.setStatus(Admin.Status.waitForCheck);
//            user.setPassword(SecurityUtils.encodePassword(user.getPassword()));
//            System.out.println("加密后："+SecurityUtils.encodePassword(user.getPassword()));
            Boolean u=adminService.save(admin);
            if (u==true){
                return new Result(Code.SAVE_OK,"管理员注册成功,请等待其它管理员审核！");
            }else {
                return new Result(Code.SAVE_ERR,"管理员注册失败！");
            }
    }


    //管理员登录
    @PostMapping("/login/admin")
    public Result adminLogin(@RequestBody Admin admin){
        return adminService.adminLogin(admin);
    }

    @PostMapping("/admin/update-password")
    @Transactional
    public Result adminUpdatePassword(@RequestBody AdminExt adminExt){
        if(adminExt.getOldPassword()==null || adminExt.getOldPassword().equals("")){
            return new Result(Code.SELECT_ERR,"原密码不能为空");
        }
        if(adminExt.getPhone()==null || adminExt.getPhone().equals("")){
            return new Result(Code.SELECT_ERR,"手机号不能为空");
        }
        if(adminExt.getPassword()==null || adminExt.getPassword().equals("")){
            return new Result(Code.SELECT_ERR,"新密码不能为空");
        }
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getId,adminExt.getId());
        queryWrapper.eq(Admin::getPhone,adminExt.getPhone());
//        queryWrapper.eq(Admin::getUsername,adminExt.getUsername());
//        queryWrapper.eq(Admin::getPassword,encoder.encode(adminExt.getOldPassword()));
        Integer count = adminMapper.selectCount(queryWrapper);
        if(count == 0){
            return new Result(Code.SELECT_ERR,"手机号错误！");
        }else if(count > 1){
            return new Result(Code.SELECT_ERR,"该用户重复，请联系管理员");
        }
        Admin admin1 = adminMapper.selectOne(queryWrapper);



        boolean matches = passwordEncoder.matches(adminExt.getOldPassword(),admin1.getPassword());

        if (matches) {
            // 密码验证通过
            boolean matches1 = passwordEncoder.matches(adminExt.getPassword(),admin1.getPassword());
            if (matches1) {
                return new Result(Code.UPDATE_ERR,"新密码不能与原密码相同！");
            }
            admin1.setPassword(encoder.encode(adminExt.getPassword()));
            //System.out.println("新密码是："+adminExt.getPassword());
            adminMapper.updateById(admin1);
            return new Result(Code.UPDATE_OK,"密码修改成功！");
        }
        return new Result(Code.UPDATE_ERR,"原密码错误！");
    }

    @PostMapping("/admin/reset-password")
    public Result resetAdminPassword(@RequestBody AdminExt adminExt){
//        System.out.println("adminExt传来"+adminExt);
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername,adminExt.getUsername()+"adminRole");
        queryWrapper.eq(Admin::getPhone,adminExt.getPhone());
        Integer count = adminMapper.selectCount(queryWrapper);
        if(count==0){
            return new Result(Code.SELECT_ERR,"用户名或手机号错误");
        }else if(count>1){
            return new Result(Code.SELECT_ERR,"该用户重复，请联系管理员");
        }
        Admin admin1 = adminMapper.selectOne(queryWrapper);
        if(admin1==null){
            return new Result(Code.SELECT_ERR,"查不到该账户信息");
        }

        admin1.setPassword(encoder.encode(adminExt.getResetPassword()));
        adminService.updateById(admin1);
        return new Result(Code.UPDATE_OK,"密码重置成功！");
    }

    //员工登录
    @PostMapping("/login/employee")
    public Result employeeLogin(@RequestBody CommonUser commonUser){
        //System.out.println("登录？？？？");
        return commonUserService.employeeLogin(commonUser);
    }


   //员工注册
    @PostMapping("/register")
    public Result employeeRegister(@RequestBody CommonUser commonUser){
        return commonUserService.register(commonUser);
    }

    @PostMapping("/verifyUser")
    public Result employeeVerifyUser(@RequestBody CommonUserExt commonUserExt){
        if(commonUserExt.getPhone()==null && commonUserExt.getPhone().equals("")){
            return new Result(Code.SELECT_ERR,"请输入手机号");
        }
        if(commonUserExt.getUsername()==null && commonUserExt.getUsername().equals("")){
            return new Result(Code.SELECT_ERR,"请输入用户名");
        }
        if(commonUserExt.getEmail()==null && commonUserExt.getEmail().equals("")){
            return new Result(Code.SELECT_ERR,"请输入邮箱");
        }

        LambdaQueryWrapper<CommonUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommonUser::getUsername,commonUserExt.getUsername());
        queryWrapper.eq(CommonUser::getPhone,commonUserExt.getPhone());
        Integer count = commonUserMapper.selectCount(queryWrapper);
        if(count==0){
            return new Result(Code.SELECT_ERR,"手机号或用户名错误");
        }
        if(count>1){
            return new Result(Code.SELECT_ERR,"该账号重复，请联系管理员");
        }
        CommonUser commonUser = commonUserMapper.selectOne(queryWrapper);
        if(commonUser.getStatus().equals("禁用")){
            return new Result(Code.SELECT_ERR,"该账号已被禁用！");
        }

        LambdaQueryWrapper<Employee> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Employee::getPhone,commonUserExt.getPhone());
            Integer count1 = employeeMapper.selectCount(queryWrapper1);
            queryWrapper1.eq(Employee::getEmail,commonUserExt.getEmail());
            Integer count2 = employeeMapper.selectCount(queryWrapper1);
            if(count1==0){
                return new Result(Code.SELECT_ERR,"该手机号未注册员工");
            }
            if(count1>1){
                return new Result(Code.SELECT_ERR,"该手机号重复，请联系管理员");
            }
            if(count2==0){
                return new Result(Code.SELECT_ERR,"邮箱错误");
            }
            if(count2>1){
                return new Result(Code.SELECT_ERR,"该邮箱重复，请联系管理员");
            }
            Employee employee = employeeMapper.selectOne(queryWrapper1);
            if(employee.getStatus().equals("离职")){
                return new Result(Code.SELECT_ERR,"你已离职！");
            }
        return new Result(Code.SELECT_OK,"信息正确");
    }

    @PostMapping("/admin/verifyUser")
    public Result adminVerifyUser(@RequestBody AdminExt adminExt){
        if(adminExt.getPhone()==null && adminExt.getPhone().equals("")){
            return new Result(Code.SELECT_ERR,"请输入手机号");
        }
        if(adminExt.getUsername()==null && adminExt.getUsername().equals("")){
            return new Result(Code.SELECT_ERR,"请输入用户名");
        }

        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername,adminExt.getUsername()+"adminRole");
        queryWrapper.eq(Admin::getPhone,adminExt.getPhone());
        Integer count = adminMapper.selectCount(queryWrapper);
        if(count==0){
            return new Result(Code.SELECT_ERR,"手机号或用户名错误");
        }
        if(count>1){
            return new Result(Code.SELECT_ERR,"该账号重复，请联系管理员");
        }

        return new Result(Code.SELECT_OK,"信息正确");
    }

    @PostMapping("/update-password")
    @Transactional
    public Result updatePassword(@RequestBody CommonUserExt commonUserExt){
        if(commonUserExt.getOldPassword()==null || commonUserExt.getOldPassword().equals("")){
            return new Result(Code.SELECT_ERR,"原密码不能为空");
        }
        if(commonUserExt.getPhone()==null || commonUserExt.getPhone().equals("")){
            return new Result(Code.SELECT_ERR,"手机号不能为空");
        }
        if(commonUserExt.getPassword()==null || commonUserExt.getPassword().equals("")){
            return new Result(Code.SELECT_ERR,"新密码不能为空");
        }
        LambdaQueryWrapper<CommonUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommonUser::getId,commonUserExt.getId());
        queryWrapper.notIn(CommonUser::getStatus,"禁用");
        Integer count = commonUserMapper.selectCount(queryWrapper);
        if(count==0){
            return new Result(Code.SELECT_ERR,"该用户已被禁用");
        }else if(count>1){
            return new Result(Code.SELECT_ERR,"该用户重复，请联系管理员");
        }
        CommonUser commonUser1 = commonUserMapper.selectOne(queryWrapper);

        if(!commonUser1.getPhone().equals(commonUserExt.getPhone())){
            return new Result(Code.SELECT_ERR,"手机号错误");
        }
        LambdaQueryWrapper<Employee> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Employee::getPhone,commonUserExt.getPhone());
        Integer count1 = employeeMapper.selectCount(queryWrapper1);
        if(count1==0){
            return new Result(Code.SELECT_ERR,"查不到该员工信息");
        }else if(count1 > 1){
            return new Result(Code.SELECT_ERR,"该员工重复，请联系管理员");
        }
        Employee employee = employeeMapper.selectOne(queryWrapper1);
        if(employee==null){
            return new Result(Code.SELECT_ERR,"查不到该员工信息");
        }

        boolean matches = passwordEncoder.matches(commonUserExt.getOldPassword(),
                commonUser1.getPassword());

        if (matches) {
            // 密码验证通过
            boolean matches1 = passwordEncoder.matches(commonUserExt.getPassword(),commonUser1.getPassword());
            if (matches1) {
                return new Result(Code.UPDATE_ERR,"新密码不能与原密码相同！");
            }
            commonUser1.setPassword(encoder.encode(commonUserExt.getPassword()));
            commonUserService.updateById(commonUser1);
            return new Result(Code.UPDATE_OK,"密码修改成功！");
        }
        return new Result(Code.UPDATE_ERR,"原密码错误");
    }

    @PostMapping("/reset-password")
    @Transactional
    public Result resetPassword(@RequestBody CommonUserExt commonUserExt){
//        System.out.println("commonUser传来"+commonUser);
        LambdaQueryWrapper<CommonUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommonUser::getUsername,commonUserExt.getUsername());
        queryWrapper.eq(CommonUser::getPhone,commonUserExt.getPhone());
        Integer count = commonUserMapper.selectCount(queryWrapper);
        if(count==0){
            return new Result(Code.SELECT_ERR,"用户名错误");
        }else if(count>1){
            return new Result(Code.SELECT_ERR,"该用户重复，请联系管理员");
        }
        CommonUser commonUser1 = commonUserMapper.selectOne(queryWrapper);
        if(commonUser1==null){
            return new Result(Code.SELECT_ERR,"查不到该账户信息");
        }

        LambdaQueryWrapper<Employee> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Employee::getPhone,commonUserExt.getPhone());
        Integer count1 = employeeMapper.selectCount(queryWrapper1);
        if(count1==0){
            return new Result(Code.SELECT_ERR,"请先填写入职信息");
        }else if(count1 > 1){
            return new Result(Code.SELECT_ERR,"该员工信息重复，请联系管理员");
        }

        queryWrapper1.eq(Employee::getEmail,commonUserExt.getEmail());
        Integer count2 = employeeMapper.selectCount(queryWrapper1);
        if(count2==0){
            return new Result(Code.SELECT_ERR,"邮箱错误");
        }else if(count2 > 1){
            return new Result(Code.SELECT_ERR,"该员工信息重复，请联系管理员");
        }

        Employee employee = employeeMapper.selectOne(queryWrapper1);
        if(employee==null){
            return new Result(Code.SELECT_ERR,"查不到该员工信息");
        }

        commonUser1.setPassword(encoder.encode(commonUserExt.getResetPassword()));
        commonUserService.updateById(commonUser1);
        return new Result(Code.UPDATE_OK,"密码重置成功！");
    }


    //根据手机号查询员工
    @GetMapping("/selectByPhone")
    public Result selectByPhone(String phone){
        return commonUserService.selectByPhone(phone);
    }

    //注册完成后，初次登陆，填写信息
    @PostMapping("/addUser")
    public Result addUser(@RequestBody Employee employee, HttpServletRequest request){
        try {
            commonUserService.addUser(employee, request);
            return new Result(Code.SAVE_OK,"信息提交成功！");
            //，请等待审核通过后登录
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Code.SAVE_ERR,"提交失败！该邮箱已注册");
        }
    }


    //通过手机号查询员工的姓名部门和手机号，返回到个人页面页面
    @GetMapping("/getUserBasicInfo")
    public Result getUserBasicInfo(String phone) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getPhone,phone);
        int count = employeeMapper.selectCount(queryWrapper);
        if(count > 1){
            return new Result(Code.SELECT_ERR,"该手机号已注册多个员工，请联系管理员");
        }else if(count == 0){
            return new Result(Code.SELECT_ERR,"找不到员工信息");
        }
        Employee employee = employeeMapper.selectOne(queryWrapper);
        EmployeeExt employeeExt = new EmployeeExt();
        //属性迁移
        Position position = null;
        if(employee!=null){
            BeanUtils.copyProperties(employee, employeeExt);
            position = positionService.getById(employee.getPositionId());
        }

        if(position != null) {
            employeeExt.setPosition(position.getPname());
        }
//        LambdaQueryWrapper<DepartLocation> queryWrapper1 = new LambdaQueryWrapper<>();
//        queryWrapper1.eq(DepartLocation::getDepartmentId,department.getId());
//        DepartLocation departLocation = departLocationMapper.selectOne(queryWrapper1);
//        if(departLocation!=null){
//            employeeExt.setLatitude(departLocation.getLatitude());
//            employeeExt.setLongitude(departLocation.getLongitude());
//        }
        if (employee!=null){
            return new Result(Code.SELECT_OK,employeeExt);
        }else {
            return new Result(Code.SELECT_ERR,"信息初始化失败！");
        }
    }



    //通过手机号查询员工信息
    @GetMapping("/selectUserInfo")
    public Result selectUserInfo(String phone) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getPhone,phone);
        int count = employeeMapper.selectCount(queryWrapper);
        if(count == 0) {
            return new Result(Code.SELECT_ERR,"查不到该员工信息");
        }
        Employee employee = employeeMapper.selectOne(queryWrapper);
        EmployeeExt employeeExt = new EmployeeExt();

        BeanUtils.copyProperties(employee,employeeExt);
        //System.out.println("属性搬完了吗："+employeeExt);

        Position position = positionService.getById(employee.getPositionId());
        if(position != null) {
            employeeExt.setPosition(position.getPname());
        }
        if (employee!=null){
            return new Result(Code.SELECT_OK,employeeExt);
        }
        else {
            return new Result(Code.SELECT_OK,"信息初始化失败！");
        }
    }

    //判断该员工账号是否可用
    @GetMapping("/judgeStatus/{userId}")
    public Result judgeStatus(@PathVariable Integer userId){//,HttpServletRequest request
//        CommonUser commonUser = commonUserMapper.selectById(userId);
//        String employeeInSession = (String) request.getSession().getAttribute("employee");
//        System.out.println("employeeInSession是: "+employeeInSession);
//        if(!employeeInSession.equals(commonUser.getUsername())){
//            System.out.println("前端执行退出");
//        }
        return commonUserService.judgeStatus(userId);
    }

    @GetMapping("/selectUsername")
    public Result selectUsername(String username){
        return commonUserService.selectUsername(username);
    }

    @GetMapping("/selectAdminUsername")
    public Result selectAdminUsername(String username){
        return commonUserService.selectAdminUsername(username);
    }

    @GetMapping("/logout")
    public Result logout(String username, HttpServletRequest request){
        try {
//            System.out.println("退出传来的username:"+username);
            LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Admin::getUsername,username);
            int count = adminMapper.selectCount(queryWrapper);

            LambdaQueryWrapper<CommonUser> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(CommonUser::getUsername,username);
            int count1 = commonUserMapper.selectCount(queryWrapper1);

            if (count1 == 0 && count > 0 && username.contains("adminRole")){//username.contains("admin")
                request.getSession().removeAttribute("admin");
//                System.out.println("logout清空管理员的Session");
                return new Result(Code.LOGOUT_OK,"退出登录成功！");
            }else if(count == 0 && count1 > 0 && !username.contains("adminRole")){
                request.getSession().removeAttribute("employee");
//                System.out.println("logout清空普通员工的Session");
                return new Result(Code.LOGOUT_OK,"退出登录成功！");
            }else {
                return new Result(Code.LOGOUT_ERR,"退出登录失败！");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(Code.LOGIN_ERR,"退出登录失败！");
        }
    }
}
